<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/fea/stringMisc.php'; ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/accHestia/src/svr/com/acc_conn.php'; ?>

<?php
    function dorpc_autogen_company($data, &$res) {
        $regionArr = file($_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/init/test_v/mt/region_src.txt");
        $type1Arr = file($_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/init/test_v/mt/companyType1.txt");
        $type2Arr = file($_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/init/test_v/mt/companyType2.txt");
        $nameArr = file($_SERVER['DOCUMENT_ROOT'] . "/accHestia/src/svr/init/test_v/mt/name.txt");
        $regionCnt = count($regionArr);
        $type1ArrCnt = count($type1Arr);
        $type2ArrCnt = count($type2Arr);
        $nameArrCnt = count($nameArr);
        srand(time());
        
        $genCnt = $data["genCount"]; //要生成随机姓名的个数
        
        $ret = '<table style="width:80%"><tr><th>辅助码</th> <th>公司名</th> <th>角色</th> </tr>';
        $w_str = "";
        for($i = 0; $i < $genCnt; ++$i) {
            $rndRegion = rand(0, $regionCnt - 1);
            $rndType1 = rand(0, $type1ArrCnt - 1);
            $rndType2 = rand(0, $type2ArrCnt - 1);
            $rndName1 = rand(0, $nameArrCnt - 1);
            $rndName2 = rand(0, $nameArrCnt - 1);

            $regionStr = rtrim($regionArr[$rndRegion]);
            $type1Str = rtrim($type1Arr[$rndType1]);
            $type2Str = rtrim($type2Arr[$rndType2]);
            $name1Str = rtrim($nameArr[$rndName1]);
            $name2Str = rtrim($nameArr[$rndName2]);
            $compName = $regionStr . $name1Str . $name2Str . $type1Str . $type2Str;
            
            $isCustomer = 0;
            $isSupplier = 0;
            $role_str = "";
            
            $roleRnd = rand(0, 100);
            if($roleRnd > 95) {
                $isCustomer = 1;
                $isSupplier = 1;
                $role_str = "客户/供应商";
            } else if($roleRnd > 40) {
                $isCustomer = 1;
                $isSupplier = 0;
                $role_str = "客户";
            } else {
               $isCustomer = 0;
               $isSupplier = 1;
               $role_str = "供应商";
            }
            
            $w_str .= "insert into companys(id, abbr, name, isCustomer, isSupplier) values(" . 
                ($i + 1) . ", '" . getAlphas($compName) . "', '" . $compName . "', '" .
                $isCustomer . "', '" . $isSupplier . "');\r\n";
            if($i % 2) {
                $ret .= "<tr><td>" . getAlphas($compName) . "</td> <td>" . $compName . "</td> <td>" . $role_str . "</td></tr>";
            } else {
                $ret .= "<tr class='acc_ood_row'><td>" . getAlphas($compName) . "</td> <td>" . $compName . "</td> <td>" . $role_str . "</td></tr>";
            }
        }
        $ret .= "</table>";
        
        $fpath = $_SERVER['DOCUMENT_ROOT'] . "/accHestia/db/test/companyDummy.sql";
        $f = fopen($fpath, "w");
        if(!$f) {
            set_res_NG($res, "failed on creating file: " . $fpath);
            return;
        }
        fwrite($f, $w_str);
        fclose($f);

        $res["hData"] = $ret;
        set_res_OK($res);
    }

    function dorpc_add_gened_compay($data, &$res) {
        $fpath = $_SERVER['DOCUMENT_ROOT'] . "/accHestia/db/test/companyDummy.sql";
        $sqlArr = file($fpath);
        $sql_cnt = count($sqlArr);

        $db = new acc_DB();
        for($i = 0; $i < $sql_cnt; $i++) {
            $ssql = rtrim($sqlArr[$i]);
            if (!$db->query($ssql)) {
                set_res_NG($res, $ssql);
                return;
            }
        }
        $res["ok_count"] = $sql_cnt;
        set_res_OK($res);
    }
?>
